#pragma once
#include <functional>
#include <algorithm>
#include <iterator>
#include <math.h>

#define RGBA(r,g,b,a) ((a)<<24|(b)<<16|(g)<<8|(r)<<0)
#define _heapChk (_heapchk()==_HEAPOK)

const float MPI = 3.1415f;

template<class T>
T logbase(T x, T base) 
{
	return (T)(log((double)x)/log((double)base));
}

template <class T>
int sign(T t)
{
	return (t>0?1:(t<0?-1:0));
}

template <class T>
T degreeToRad(T degree)
{
	while (degree>360)
	{
		degree -= 360;
	}
	return (T)(MPI)*degree/180;
}

template <class T>
T LERP( T a, T b, T alpha)
{
	return a + (b - a) * alpha;
}

/**
 *	generate rand number between min to max
 */
template <class T>
T randRange(T min, T max)
{
	assert( min <= max );

	if ( min == max )
	{
		return min;
	}
	return ((T)rand() / (RAND_MAX + 1) * (max - min) + min);
}

template< typename BidirectionalIterator >
void quick_sort( BidirectionalIterator first, BidirectionalIterator last ) 
{
	if( first != last ) {
		BidirectionalIterator left  = first;
		BidirectionalIterator right = last;
		BidirectionalIterator pivot = left++;

		while( left != right ) {
			if( ( *left <= *pivot ) ) {
				++left;
			} else {
				while( (left != right) && ( *pivot <= *right ) )
					right--;
				std::iter_swap( left, right );
			}
		}

		if ( *pivot <= *left )
			--left;
		std::iter_swap( first, left );

		quick_sort( first, left );
		quick_sort( right, last );
	}
}